package org.example.com.kotlinlab.delegate.property_delegate.prop_map_delegate

/**
 * 通过属性委托，把类属性和Map进行映射。
 * 需要注意：
 * 1.类的属性名和Map中的字段名必须匹配.
 * 2.属性修改对双方都有影响。
 * 3.如果是只读属性，使用Map即可；如果是可写属性，使用MutableMap
 *
 */

class User(val map:MutableMap<String, Any>){
    var name: String by map
    var age: Int by map
}

fun main() {
    val map:MutableMap<String,Any> = mutableMapOf(
        "name" to "John",
        "age" to 30
    )
    val user = User(map)
    println(map)
    println("name = ${user.name}, age = ${user.age}")

    map["name"] = "Hello"
    println(map)
    println("name = ${user.name}, age = ${user.age}")
    user.name = "Wowo"
    println(map)
    println("name = ${user.name}, age = ${user.age}")
}